package com.pi.dc.facade;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.pi.base.util.math.MathUtil;
import com.pi.dc.dao.entity.DcBedNatureEntity;
import com.pi.dc.dao.entity.DcDeviceInfoEntity;
import com.pi.dc.vo.UserBedInfoVo;
import com.pi.dc.vo.UserBedSketch;
import com.pi.uc.dao.entity.UcUserEntity;
import com.pi.uc.service.UcUserService;

@Service
public class UserBedFacade {
  
  @Autowired
  private UcUserService userService;

  private static final int DEFAULT_OFFLINE_TIME = 600000;//默认600秒没上传数据则离线
  
  private static final int OFFLINE_TIME_UNUSUAL = 86400000;//默认600秒没上传数据则离线
  
  public UserBedSketch translateToUserBedSketch(
      List<UserBedInfoVo> bedList, long count, Long userId){
    UserBedSketch sketch = new UserBedSketch();
    sketch.setTotalUser(count);
    sketch.setDevInfoList(new ArrayList<>());
    if(0L == userId) {
      sketch.setName(StringUtils.EMPTY);
      sketch.setAvatar(StringUtils.EMPTY);
      return sketch;
    }
    UcUserEntity userInfo = userService.queryUserInfo(userId);
    sketch.setName(userInfo.getName());
    sketch.setAvatar(userInfo.getAvatar());
    if(CollectionUtils.isEmpty(bedList)){
      return sketch;
    }
    sketch.getDevInfoList().addAll(bedList);
    return sketch;
  }
  
  public UserBedInfoVo transLateBedEntityToVO(DcDeviceInfoEntity entity, DcBedNatureEntity nature) {
    UserBedInfoVo vo = new UserBedInfoVo();
    vo.setDeviceId(entity.getId());
    vo.setDeviceName(entity.getDevName());
    if(null != nature) {
      vo.setRelativeHumidity(String.valueOf(nature.getRelativeHumidity()));
      vo.setTemperature(String.valueOf(nature.getTemperature()));
      vo.setBatteryStatus(null != nature.getBatteryStatus()?MathUtil.getIntervalValue((nature.getBatteryStatus()-13)*14, 0, 100).intValue():0);
      //查过 DEFAULT_OFFLINE_TIME 的设定值则算离线
      if(null == nature.getBatteryStatus() || nature.getWifiStatus() == 0){
        vo.setDeviceStatus("未配置");
      } else if(nature.getWifiStatus() == 1){
        vo.setDeviceStatus("待下发");
      }else {
        Long offlineTime = System.currentTimeMillis() - nature.getLatestActiveTime();
        if(null == nature.getLatestActiveTime() || offlineTime> DEFAULT_OFFLINE_TIME){
          if(offlineTime > OFFLINE_TIME_UNUSUAL) {
            vo.setDeviceStatus("离线且超过一天");
          }else {
            vo.setDeviceStatus("已配置, 离线");
          }
        }else{
            vo.setDeviceStatus("在线");
        } 
      }
    }else {
      vo.setDeviceStatus("未配置");
    }
    return vo;
  }
  
  public List<UserBedInfoVo> transLateBedEntitiesToVO(List<DcDeviceInfoEntity> deviceInfoList, List<DcBedNatureEntity> natures) {
    if(CollectionUtils.isEmpty(deviceInfoList)) {
      return new ArrayList<>(0);
    }
    Map<Long, DcBedNatureEntity> bedNatureMap;
    if(CollectionUtils.isEmpty(natures)) {
      bedNatureMap = new HashMap<>(0);
    }else {
      bedNatureMap = natures.stream().collect(Collectors.toMap(DcBedNatureEntity::getId, o -> o));
    }
    List<UserBedInfoVo> voList = new ArrayList<>(deviceInfoList.size());
    deviceInfoList.forEach(entity -> {
      voList.add(transLateBedEntityToVO(entity, bedNatureMap.get(entity.getId())));
    });
    return voList;
  }
}
